package org.yonsei.iwb.sdt.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import org.netbeans.api.visual.action.ActionFactory;
import org.netbeans.api.visual.graph.layout.GraphLayoutFactory;
import org.netbeans.api.visual.graph.layout.TreeGraphLayout;
import org.netbeans.api.visual.layout.LayoutFactory;
import org.netbeans.api.visual.widget.LabelWidget;
import org.netbeans.api.visual.widget.LayerWidget;
import org.netbeans.api.visual.widget.Scene;
import org.netbeans.api.visual.widget.Widget;

public class TreeTestFrame {

    private void showGraph() {
        SDTGraphScene scene = new SDTGraphScene();
        Dimension maximumSize = new Dimension();
        maximumSize.setSize(200, 50);

        String oneNodeID = ">0 hasSibling , T=x: F=y" + "\\n" + "1";
        String twoNodeID = "T=5 : F=0" + "\\\n" + "2";
        String threeNodeID = "T=0 : F=5" + "\\n" + "3";
        String fourNodeID = "<html>if multi JLabel<br>use </html>";
        String edge1 = "edge1";
        String edge2 = "edge2";
        String edge3 = "edge3";
        System.out.println(oneNodeID);
        System.out.println();
        System.out.println(twoNodeID);
        System.out.println();
        System.out.println(threeNodeID);
        System.out.println();
        System.out.println(fourNodeID);
        Widget one = scene.addNode(oneNodeID);
        Widget two = scene.addNode(twoNodeID);
        Widget three = scene.addNode(threeNodeID);
        Widget four = scene.addNode(fourNodeID);
        Widget five = scene.addNode("testnode" + "\n" + "a");

        one.setToolTipText("tool tip" + "\n" + "next line");
        
        

//        one.addChild(new LabelWidget(scene, "The anchor ProxyAnchor."));
//        one.addChild(new LabelWidget(scene, "Taaaaa"));
//        one.addChild(2, new LabelWidget(scene, "bbb"));

        one.setMaximumSize(maximumSize);
        one.setMinimumSize(maximumSize);
        two.setMaximumSize(maximumSize);
        two.setMinimumSize(maximumSize);
        three.setMaximumSize(maximumSize);
        three.setMinimumSize(maximumSize);
        five.setMaximumSize(maximumSize);
        five.setMinimumSize(maximumSize);
        
        
        
        one.setEnabled(false);
        one.setOpaque(true);
//	one.getActions().removeAction(ActionFactory.createMoveAction ());
//	two.getActions().removeAction(ActionFactory.createMoveAction ());
//	three.getActions().removeAction(ActionFactory.createMoveAction ());
        scene.setEdgeColor(Color.RED);
//        scene.addEdge(edge1);
        Widget childEdgeWidget = scene.addEdge(edge1);
        
        
        scene.setEdgeColor(Color.BLUE);
        scene.addEdge(edge2);
        scene.addEdge(edge3);
        String s = oneNodeID;
        scene.setEdgeSource(edge1, s);
        scene.setEdgeTarget(edge1, twoNodeID);
        scene.setEdgeSource(edge2, s);
        scene.setEdgeTarget(edge2, threeNodeID);
        scene.setEdgeSource(edge3, threeNodeID);
        scene.setEdgeTarget(edge3, fourNodeID);

        int x = 150;
        int y = 100;
        int value = 50;

//        one.setPreferredLocation(new Point(x, y));
//        two.setPreferredLocation(new Point(x + value, y + value));
//        three.setPreferredLocation(new Point(x - value, y + value));
//        four.setPreferredLocation(new Point(x - value * 2, y + value * 2));
        TreeGraphLayout tgl = new TreeGraphLayout(scene, 100, 100, 50, 50, true);
        tgl.layout(oneNodeID);
        
        
//        three.setLayout(LayoutFactory.createHorizontalFlowLayout(LayoutFactory.SerialAlignment.CENTER, 20));
//        four.setLayout(LayoutFactory.createHorizontalFlowLayout(LayoutFactory.SerialAlignment.CENTER, 20));
//	SceneSupport.show(scene.createView());
        JComponent createView = scene.createView();
        show(createView);
    }

    public void show(JComponent sceneView) {
        JScrollPane panel = new JScrollPane(sceneView);
        panel.getHorizontalScrollBar().setUnitIncrement(32);
        panel.getHorizontalScrollBar().setBlockIncrement(256);
        panel.getVerticalScrollBar().setUnitIncrement(32);
        panel.getVerticalScrollBar().setBlockIncrement(256);

        showCoreEDT(sceneView);
//		int width=800,height=600;
//		JFrame frame = new JFrame ();//new JDialog (), true);
//		frame.add (view, BorderLayout.CENTER);
//		frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
//		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
//		frame.setBounds((screenSize.width-width)/2, (screenSize.height-height)/2, width, height);
//		frame.setVisible (true);
    }

    private void showCoreEDT(JComponent view) {
        int width = 800, height = 600;
        JFrame frame = new JFrame();//new JDialog (), true);
        frame.add(view, BorderLayout.CENTER);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        frame.setBounds((screenSize.width - width) / 2, (screenSize.height - height) / 2, width, height);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
//		JFrame jf = new JFrame();
        TreeTestFrame tf = new TreeTestFrame();
        tf.showGraph();
    }
}
